package org.esti.controller;

import org.esti.model.AppraisalTask;
import org.esti.model.KnowledgeField;
import org.esti.model.User;
import org.esti.service.AppraisalTaskService;
import org.esti.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Artem Loginov (logart)
 */

/**
 * This controller is used for providing mapping between request and view
 * and showing different appraisal information.
 * For example: is this appraisal publish or not or date of it's creation.
 */
@Controller
public class AppraisalController {

    @Autowired
    private UserService userService;

    @Autowired
    private AppraisalTaskService appraisalTaskService;

    /**
     * Show current appraisal for user.
     * Current appraisal is topics that user must know to get "next level" of education.
     *
     * @param login of user whose appraisal we want to show
     * @param model set of parameters which will be passed to view. For example user and his appraisal.
     * @return name of view which will be render
     */
    @RequestMapping(value = "/{login}/appraisals/current")
    public String showCurrentAppraisal(@PathVariable String login, Model model) {
        User user = userService.getUserByLogin(login);

        model.addAttribute("user", user);

        Map<KnowledgeField, List<AppraisalTask>> appraisalTasks = appraisalTaskService.getAppraisalTasksByLevelId(user.getLevel().getId());

        for (KnowledgeField knowledgeField : appraisalTasks.keySet()) {
            Logger.getLogger("lol1").log(Level.INFO, knowledgeField.getName() + '\n');
            for (AppraisalTask task : appraisalTasks.get(knowledgeField)) {
                Logger.getLogger("lol1").log(Level.INFO, task.getSubject() + '\n');
            }
        }

        model.addAttribute("appraisalTasks", appraisalTasks);

        return "current_appraisal";
    }
}
